package org.mookt.unit;

import com.google.common.base.Objects;

/**
 * <p>
 * This class represents the dimension of an {@linkplain Unit}. Two units
 * {@code u1} and {@code u2} are said to be compatible if they have the same
 * dimension.
 * </p>
 * 
 * <p>
 * This class is immutable and thread-safe.
 * </p>
 * 
 * @author Ricardo Engel
 * @see <a href="http://en.wikipedia.org/wiki/Dimensional_analysis"> Wikipedia:
 *      Dimensional Analysis</a>
 */
public class Dimension {

    private final char symbol;

    /**
     * Constructor, creating a new currency instance.
     * 
     * @param symbol the dimension symbol
     */
    private Dimension(final char symbol) {
        this.symbol = symbol;
    }

    /**
     * Returns a {@code Dimension} instance with the given symbol.
     * 
     * @param symbol the dimension symbol
     */
    public static Dimension of(final char symbol) {
        return new Dimension(symbol);
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        return Objects.hashCode(symbol);
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(final Object obj) {
        if (obj == this) {
            return true;
        } else if (obj instanceof Dimension) {
            final Dimension that = (Dimension) obj;
            return symbol == that.symbol;
        }

        return false;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return Character.toString(symbol);
    }

}